package com.huarui.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class DateJsonValueProcessor implements JsonValueProcessor {
	private static final String DEFAULT_DATA_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private DateFormat dateFormat;
	public DateJsonValueProcessor(){
		dateFormat=new SimpleDateFormat(DEFAULT_DATA_FORMAT);
	}
	public DateJsonValueProcessor(String dataPattern){
		try {
			dateFormat=new SimpleDateFormat(dataPattern);
		} catch (Exception e) {
			dateFormat=new SimpleDateFormat(DEFAULT_DATA_FORMAT);
		}
	}
	@Override
	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	@Override
	public Object processObjectValue(String key, Object value,
			JsonConfig jsonConfig) {
		return process(value);
	}
	private Object process(Object value){
		return dateFormat.format((Date)value);
	}

}
